<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="78px">
      <el-form-item label="合同编号" prop="contractNumber">
        <el-input
          v-model="queryParams.contractNumber"
          placeholder="请输入合同编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input
          v-model="queryParams.contractName"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同类别" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择合同类别" clearable>
          <el-option
            v-for="dict in dict.type.village_contract_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签订日期">
        <el-date-picker
          v-model="daterangeSigningDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="所属单位" prop="unit">
        <el-select v-model="queryParams.unit" placeholder="请选择所属单位" clearable>
          <el-option
            v-for="dict in dict.type.village_unit"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="$router.push('/contractDetail')"
          v-hasPermi="['village:contracts:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['village:contracts:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['village:contracts:remove']"
        >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['village:contracts:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="合同编号" align="center" prop="contractNumber"/>
      <el-table-column label="合同名称" align="center" prop="contractName"/>
      <el-table-column label="负责人" align="center" prop="responsibleName"/>
      <el-table-column label="经办人" align="center" prop="agentName"/>
      <el-table-column label="合同类别" align="center" prop="category">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.village_contract_type" :value="scope.row.category"/>
        </template>
      </el-table-column>
      <el-table-column label="签订日期" align="center" prop="signingDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signingDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同有效状态" align="center" prop="validityStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.village_whether" :value="scope.row.validityStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="所属单位" align="center" prop="unit">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.village_unit" :value="scope.row.unit"/>
        </template>
      </el-table-column>
      <el-table-column label="进行状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.village_running_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-unfold"
            @click="skipDetail(scope.row)"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="$router.push(`/contractDetail?id=${scope.row.id}&type=edit`)"
            v-hasPermi="['village:contracts:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['village:contracts:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


  </div>
</template>

<script>
import { listContracts, getContracts, delContracts, addContracts, updateContracts } from '@/api/village/contracts'

export default {
  name: 'Contracts',
  dicts: ['village_contract_type', 'village_payment_method', 'village_assume_type', 'village_unit', 'village_running_status', 'village_degree', 'village_whether', 'village_responsible_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedVillageMember: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同信息管理表格数据
      contractsList: [],
      // 合同成员表格数据
      villageMemberList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeSigningDate: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNumber: null,
        contractName: null,
        category: null,
        signingDate: null,
        unit: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
  },
  methods: {
    skipDetail(row) {
      this.$router.push(`/detail?id=${row.id}`)
    },
    /** 查询合同信息管理列表 */
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (null != this.daterangeSigningDate && '' != this.daterangeSigningDate) {
        this.queryParams.params['beginSigningDate'] = this.daterangeSigningDate[0]
        this.queryParams.params['endSigningDate'] = this.daterangeSigningDate[1]
      }
      listContracts(this.queryParams).then(response => {
        this.contractsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractNumber: null,
        contractName: null,
        responsibleName: null,
        responsibleEmail: null,
        agentName: null,
        agentEmail: null,
        category: null,
        signingDate: null,
        validityStatus: null,
        paymentMethod: null,
        isCounterpartySealed: null,
        responsibleType: null,
        phone: null,
        unit: null,
        funds: null,
        isFirstpartySealed: null,
        terminationTime: null,
        commencedTime: null,
        status: null,
        createdTime: null,
        updatedTime: null,
        createdBy: null,
        updatedBy: null,
        electronicRecords: null
      }
      this.villageMemberList = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeSigningDate = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加合同信息管理'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      this.$router.push(`/contractDetail?id=${id}&type=edit`)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.form.villageMemberList = this.villageMemberList
          if (this.form.id != null) {
            updateContracts(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addContracts(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除合同名称为"' + row.contractName + '"的数据项？').then(function() {
        return delContracts(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 合同成员序号 */
    rowVillageMemberIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 合同成员添加按钮操作 */
    handleAddVillageMember() {
      let obj = {}
      obj.memberType = ''
      obj.name = ''
      obj.jobNumber = ''
      obj.workUnit = ''
      obj.position = ''
      obj.degree = ''
      obj.assumeType = ''
      this.villageMemberList.push(obj)
    },
    /** 合同成员删除按钮操作 */
    handleDeleteVillageMember() {
      if (this.checkedVillageMember.length == 0) {
        this.$modal.msgError('请先选择要删除的合同成员数据')
      } else {
        const villageMemberList = this.villageMemberList
        const checkedVillageMember = this.checkedVillageMember
        this.villageMemberList = villageMemberList.filter(function(item) {
          return checkedVillageMember.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleVillageMemberSelectionChange(selection) {
      this.checkedVillageMember = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('village/contracts/export', {
        ...this.queryParams
      }, `contracts_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
